package com.andnnl.list;

import cn.hutool.json.JSONUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 并列排名
 *
 * Created by chenss on 2023/12/6.
 */
public class PersonSortTest {
    public static void main(String[] args) {
        System.out.println(new Date().toString().substring(0,7));
        // 根据name去重
        List<Person> persons = new ArrayList<>();
        persons.add(new Person("13", "name3"));
        persons.add(new Person("11", "name1"));
        persons.add(new Person("14", "name42"));
        persons.add(new Person("13", "name322"));
        persons.add(new Person("14", "name41"));
        persons.add(new Person("12", "name2"));
        persons.add(new Person("14", "name43"));
        persons.add(new Person("15", "name5"));

        persons = persons.stream().sorted((p1, p2) -> Integer.parseInt(p2.getId()) - Integer.parseInt(p1.getId())).collect(Collectors.toList());
        //相同id值，并列排名
        persons.stream().reduce(new Person(), (a, b) -> {
            b.setRank(b.getId().equals(a.getId()) ? a.getRank() : a.getRank() + 1);
            return b;
        });

        for (Person person : persons) {
            System.out.println(JSONUtil.toJsonStr(person));
        }
    }
}
